﻿#include "mark_bind_dialog.h"
#include "ui_mark_bind_dialog.h"

MarkBindDialog::MarkBindDialog(QWidget* parent) :
	QDialog(parent),
	ui(new Ui::MarkBindDialog)
{
	ui->setupUi(this);

	connect(ui->pushButton_bind_, &QPushButton::clicked, this, &MarkBindDialog::BindMark);
}

MarkBindDialog::~MarkBindDialog()
{
	delete ui;
}

void MarkBindDialog::SetAlreadyAttr(const QList<QPair<QString, QString>>& attr_script_list)
{
	//添加属性到界面
	foreach(const auto & attr_script, attr_script_list)
	{
		QString name = attr_script.first;
		QString script = attr_script.second;
		//界面添加复选款
		QCheckBox* box = new QCheckBox(name);
		ui->hLayout_mark_attr_->addWidget(box);
	}
}

void MarkBindDialog::BindMark()
{
	//标签名
	QString mark_name = ui->lineEdit_mark_name_->text();
	//启用属性
	QStringList attr_enable_list;
	for (int i = 0; i < ui->hLayout_mark_attr_->count(); ++i)
	{
		QLayoutItem* child = ui->hLayout_mark_attr_->itemAt(i);
		QCheckBox* box = qobject_cast<QCheckBox*>(child->widget());
		if (box->isChecked())
		{
			attr_enable_list.append(box->text());
		}
	}
	//标签备注
	QString mark_note = ui->textEdit_mark_note_->toPlainText();
	//检查参数
	if (mark_name.isEmpty())
	{
		QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("标签名称不可为空"), QMessageBox::Ok, QMessageBox::Ok);
		return;
	}
	if (attr_enable_list.isEmpty())
	{
		QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("须勾选属性"), QMessageBox::Ok, QMessageBox::Ok);
		return;
	}

	emit MarkBindFinishedSignal(mark_name, attr_enable_list, mark_note);
	close();
}
